package xx.services.page.rest;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import xx.entity.page.PageNavigation;
import xx.services.Passport;
import xx.services.page.inter.INavigationInterface;
import xx.services.util.Constants;
import xx.util.Arr;
import xx.util.string.Format;

/**
 * page页面导航管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/page/navigation")
 * @author Administrator 2010-12-22
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/page/navigation")
public class NavigationRest extends Passport implements INavigationInterface
{

    public NavigationRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 在指定分类{category_id}下增加子分类，
     * 
     * @POST
     * @Path("/{category_id}")
     * @param categoryId-@PathParam("category_id")
     *            分组ID
     * @param name-@FormParam("name")
     *            分组名称
     * @param sortId-@FormParam("sort_category_id")
     *            兄弟节点ID（>0时，表示把新增的分组排在categoryId的后边，<0时，表示把新增的分组排在categoryId的前边，=0时，表示排在最后边）
     * @param description-@FormParam("description")
     *            分类描述
     * @param pageId-@FormParam("page_id")
     *            与分组关联的页面ID
     * @param url-@FormParam("url")
     *            关联的URL地址
     * @param target-@FormParam("target")
     *            打开窗口的方式，参见A标签的target属性值
     * @return 新分类实体json数据
     */
    @POST
    @Path("/{category_id}")
    public String add(@PathParam("category_id")
    long categoryId, @FormParam("name")
    String name, @FormParam("sort_category_id")
    long sortId, @FormParam("description")
    String description, @DefaultValue("0")
    @FormParam("page_id")
    long pageId, @FormParam("url")
    String url, @FormParam("target")
    String target)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            pn.setParentId(categoryId).setName(name).setDescription(description);
            pn.setPageId(pageId).setUrl(url).setTarget(target);
            pn.insert();

            // 把新增的分组排在categoryId的后边
            if (sortId > 0)
            {
                pn.sortAfter(sortId);
            }
            // 把新增的分组排在categoryId的前边
            else if (sortId < 0)
            {
                pn.sortBefore(sortId);
            }

            return ok(pn);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 删除分类
     * 
     * @DELETE
     * @Path ("/{category_id}")
     * @param categoryId-@FormParam("category_id")
     *            分类ID，多个ID用“_”隔开
     * @return true-删除成功，string-其他删除失败（string就是失败原因）
     */
    @DELETE
    @Path("/{category_id}")
    public String delete(@PathParam("category_id")
    String categoryId)
    {
        try
        {
            int count = 0;
            long[] categoryIdArr = Arr.toLongArray(categoryId);
            for (long id : categoryIdArr)
            {
                PageNavigation pn = new PageNavigation(qr, owner);
                pn.setCategoryId(id);
                pn.delete();
            }
            return ok(count);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取指定{categoryId}第一级分组
     * 
     * @GET
     * @Path ("/{category_id}/children")
     * @param categoryId-@PathParam("category_id")
     *            分类ID
     * @param jsonkeys-@QueryParam("jsonkeys")
     *            相关JSONkey
     * @return 分类列表
     */
    @GET
    @Path("/{category_id}/children")
    public String getChildren(@PathParam("category_id")
    long categoryId, @QueryParam("jsonkeys")
    String jsonkeys)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            pn.addJsonKey(jsonkeys);

            List list = pn.setCategoryId(categoryId).getChildrenEx();

            return ok(list);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取指定分组信息
     * 
     * @GET
     * @Path ("/{category_id}")
     * @param categoryId-@PathParam
     *            ("category_id") 分类ID
     * @return 分类实体
     */
    @GET
    @Path("/{category_id}")
    public String getInfo(@PathParam("category_id")
    long categoryId)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            pn.setCategoryId(categoryId).load();
            return ok(pn);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 修改分类
     * 
     * @PUT
     * @Path ("/{category_id}")
     * @param name-@FormParam("name")
     *            分组名称
     * @param description-@FormParam("description")
     *            分类描述
     * @param pageId-@FormParam("page_id")
     *            与分组关联的页面ID
     * @param url-@FormParam("url")
     *            关联的URL地址
     * @param target-@FormParam("target")
     *            打开窗口的方式，参见A标签的target属性值
     * @return 新分类实体json数据
     */
    @PUT
    @Path("/{category_id}")
    public String modify(@PathParam("category_id")
    long categoryId, @FormParam("name")
    String name, @FormParam("description")
    String description, @FormParam("page_id")
    long pageId, @FormParam("url")
    String url, @FormParam("target")
    String target)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            StringBuilder updateColumn = new StringBuilder();
            pn.setCategoryId(categoryId);

            if (!Format.isEmpty(name))
            {
                pn.setName(name);
                updateColumn.append(",").append(PageNavigation.NAME);
            }

            if (!Format.isEmpty(description))
            {
                pn.setDescription(description);
                updateColumn.append(",").append(PageNavigation.DESCRIPTION);
            }

            if (pageId > 0)
            {
                pn.setPageId(pageId);
                updateColumn.append(",").append(PageNavigation.PAGE_ID);
            }

            if (!Format.isEmpty(url))
            {
                pn.setUrl(url);
                updateColumn.append(",").append(PageNavigation.URL);
            }

            if (!Format.isEmpty(target))
            {
                pn.setTarget(target);
                updateColumn.append(",").append(PageNavigation.TARGET);
            }

            if (updateColumn.length() > 0)
            {
                pn.update(updateColumn.toString());
            }
            return ok(pn);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    @PUT
    @Path("/{category_id}/after/{after_id}")
    public String sortAfter(@PathParam("category_id")
    long categoryId, @PathParam("after_id")
    long afterId)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            pn.setCategoryId(categoryId);
            pn.after(afterId);
            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    @PUT
    @Path("/{category_id}/before/{before_id}")
    public String sortBefore(@PathParam("category_id")
    long categoryId, @PathParam("before_id")
    long beforeId)
    {
        try
        {
            PageNavigation pn = new PageNavigation(qr, owner);
            pn.setCategoryId(categoryId);
            pn.before(beforeId);
            return ok(true);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }
}
